<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="date" content="2022-06-19 18:30:45"><style>body {
  max-width: 980px;
  outline: 1300px solid #fff;
  margin: 16px auto;
}

body .markdown-body
{
  padding: 45px;
}

@font-face {
  font-family: fontawesome-mini;
  src: url(data:font/woff;charset=utf-8;base64,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) format('woff');
}

.markdown-body {
  font-family: sans-serif;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  color: #333333;
  overflow: hidden;
  font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  word-wrap: break-word;
}

.markdown-body a {
  background: transparent;
}

.markdown-body a:active,
.markdown-body a:hover {
  outline: 0;
}

.markdown-body b,
.markdown-body strong {
  font-weight: bold;
}

.markdown-body mark {
  background: #ff0;
  color: #000;
  font-style: italic;
  font-weight: bold;
}

.markdown-body sub,
.markdown-body sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
.markdown-body sup {
  top: -0.5em;
}
.markdown-body sub {
  bottom: -0.25em;
}

.markdown-body h1 {
  font-size: 2em;
  margin: 0.67em 0;
}

.markdown-body img {
  border: 0;
}

.markdown-body hr {
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  height: 0;
}

.markdown-body pre {
  overflow: auto;
}

.markdown-body code,
.markdown-body kbd,
.markdown-body pre,
.markdown-body samp {
  font-family: monospace, monospace;
  font-size: 1em;
}

.markdown-body input {
  color: inherit;
  font: inherit;
  margin: 0;
}

.markdown-body html input[disabled] {
  cursor: default;
}

.markdown-body input {
  line-height: normal;
}

.markdown-body input[type="checkbox"] {
  box-sizing: border-box;
  padding: 0;
}

.markdown-body table {
  border-collapse: collapse;
  border-spacing: 0;
}

.markdown-body td,
.markdown-body th {
  padding: 0;
}

.markdown-body .codehilitetable,
.markdown-body .highlighttable {
  border: 0;
  border-spacing: 0;
}

.markdown-body .codehilitetable tr,
.markdown-body .highlighttable {
  border: 0;
}

.markdown-body .codehilitetable pre,
.markdown-body .codehilitetable div.codehilite,
.markdown-body .highlighttable pre,
.markdown-body .highlighttable div.highlight {
  margin: 0;
}

.markdown-body .linenos,
.markdown-body .code,
.markdown-body .codehilitetable td,
.markdown-body .highlighttable td {
  border: 0;
  padding: 0;
}

.markdown-body td:not(.linenos) .linenodiv {
  padding: 0 !important;
}

.markdown-body .code {
  width: 100%;
}

.markdown-body .linenos div pre,
.markdown-body .linenodiv pre,
.markdown-body .linenodiv {
  border: 0;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  -webkit-border-top-left-radius: 3px;
  -webkit-border-bottom-left-radius: 3px;
  -moz-border-radius-topleft: 3px;
  -moz-border-radius-bottomleft: 3px;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;
}

.markdown-body .code div pre,
.markdown-body .code div {
  border: 0;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  -webkit-border-top-right-radius: 3px;
  -webkit-border-bottom-right-radius: 3px;
  -moz-border-radius-topright: 3px;
  -moz-border-radius-bottomright: 3px;
  border-top-right-radius: 3px;
  border-bottom-right-radius: 3px;
}

.markdown-body * {
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.markdown-body input {
  font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
  line-height: 1.4;
}

.markdown-body a {
  color: #4183c4;
  text-decoration: none;
}

.markdown-body a:hover,
.markdown-body a:focus,
.markdown-body a:active {
  text-decoration: underline;
}

.markdown-body hr {
  height: 0;
  margin: 15px 0;
  overflow: hidden;
  background: transparent;
  border: 0;
  border-bottom: 1px solid #ddd;
}

.markdown-body hr:before,
.markdown-body hr:after {
  display: table;
  content: " ";
}

.markdown-body hr:after {
  clear: both;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin-top: 15px;
  margin-bottom: 15px;
  line-height: 1.1;
}

.markdown-body h1 {
  font-size: 30px;
}

.markdown-body h2 {
  font-size: 21px;
}

.markdown-body h3 {
  font-size: 16px;
}

.markdown-body h4 {
  font-size: 14px;
}

.markdown-body h5 {
  font-size: 12px;
}

.markdown-body h6 {
  font-size: 11px;
}

.markdown-body blockquote {
  margin: 0;
}

.markdown-body ul,
.markdown-body ol {
  padding: 0;
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body ol ol,
.markdown-body ul ol {
  list-style-type: lower-roman;
}

.markdown-body ul ul ol,
.markdown-body ul ol ol,
.markdown-body ol ul ol,
.markdown-body ol ol ol {
  list-style-type: lower-alpha;
}

.markdown-body dd {
  margin-left: 0;
}

.markdown-body code,
.markdown-body pre,
.markdown-body samp {
  font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  font-size: 12px;
}

.markdown-body pre {
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body kbd {
  background-color: #e7e7e7;
  background-image: -moz-linear-gradient(#fefefe, #e7e7e7);
  background-image: -webkit-linear-gradient(#fefefe, #e7e7e7);
  background-image: linear-gradient(#fefefe, #e7e7e7);
  background-repeat: repeat-x;
  border-radius: 2px;
  border: 1px solid #cfcfcf;
  color: #000;
  padding: 3px 5px;
  line-height: 10px;
  font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
  display: inline-block;
}

.markdown-body>*:first-child {
  margin-top: 0 !important;
}

.markdown-body>*:last-child {
  margin-bottom: 0 !important;
}

.markdown-body .headerlink {
  font: normal 400 16px fontawesome-mini;
  vertical-align: middle;
  margin-left: -16px;
  float: left;
  display: inline-block;
  text-decoration: none;
  opacity: 0;
  color: #333;
}

.markdown-body .headerlink:focus {
  outline: none;
}

.markdown-body h1 .headerlink {
  margin-top: 0.8rem;
}

.markdown-body h2 .headerlink,
.markdown-body h3 .headerlink {
  margin-top: 0.6rem;
}

.markdown-body h4 .headerlink {
  margin-top: 0.2rem;
}

.markdown-body h5 .headerlink,
.markdown-body h6 .headerlink {
  margin-top: 0;
}

.markdown-body .headerlink:hover,
.markdown-body h1:hover .headerlink,
.markdown-body h2:hover .headerlink,
.markdown-body h3:hover .headerlink,
.markdown-body h4:hover .headerlink,
.markdown-body h5:hover .headerlink,
.markdown-body h6:hover .headerlink {
  opacity: 1;
  text-decoration: none;
}

.markdown-body h1 {
  padding-bottom: 0.3em;
  font-size: 2.25em;
  line-height: 1.2;
  border-bottom: 1px solid #eee;
}

.markdown-body h2 {
  padding-bottom: 0.3em;
  font-size: 1.75em;
  line-height: 1.225;
  border-bottom: 1px solid #eee;
}

.markdown-body h3 {
  font-size: 1.5em;
  line-height: 1.43;
}

.markdown-body h4 {
  font-size: 1.25em;
}

.markdown-body h5 {
  font-size: 1em;
}

.markdown-body h6 {
  font-size: 1em;
  color: #777;
}

.markdown-body p,
.markdown-body blockquote,
.markdown-body ul,
.markdown-body ol,
.markdown-body dl,
.markdown-body table,
.markdown-body pre,
.markdown-body .admonition {
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-body hr {
  height: 4px;
  padding: 0;
  margin: 16px 0;
  background-color: #e7e7e7;
  border: 0 none;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 2em;
}

.markdown-body ul ul,
.markdown-body ul ol,
.markdown-body ol ol,
.markdown-body ol ul {
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body li>p {
  margin-top: 16px;
}

.markdown-body dl {
  padding: 0;
}

.markdown-body dl dt {
  padding: 0;
  margin-top: 16px;
  font-size: 1em;
  font-style: italic;
  font-weight: bold;
}

.markdown-body dl dd {
  padding: 0 16px;
  margin-bottom: 16px;
}

.markdown-body blockquote {
  padding: 0 15px;
  color: #777;
  border-left: 4px solid #ddd;
}

.markdown-body blockquote>:first-child {
  margin-top: 0;
}

.markdown-body blockquote>:last-child {
  margin-bottom: 0;
}

.markdown-body table {
  display: block;
  width: 100%;
  overflow: auto;
  word-break: normal;
  word-break: keep-all;
}

.markdown-body table th {
  font-weight: bold;
}

.markdown-body table th,
.markdown-body table td {
  padding: 6px 13px;
  border: 1px solid #ddd;
}

.markdown-body table tr {
  background-color: #fff;
  border-top: 1px solid #ccc;
}

.markdown-body table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

.markdown-body img {
  max-width: 100%;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.markdown-body code,
.markdown-body samp {
  padding: 0;
  padding-top: 0.2em;
  padding-bottom: 0.2em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(0,0,0,0.04);
  border-radius: 3px;
}

.markdown-body code:before,
.markdown-body code:after {
  letter-spacing: -0.2em;
  content: "\00a0";
}

.markdown-body pre>code {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

.markdown-body .codehilite,
.markdown-body .highlight {
  margin-bottom: 16px;
}

.markdown-body .codehilite pre,
.markdown-body .highlight pre,
.markdown-body pre {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f7f7f7;
  border-radius: 3px;
}

.markdown-body .codehilite pre,
.markdown-body .highlight pre {
  margin-bottom: 0;
  word-break: normal;
}

.markdown-body pre {
  word-wrap: normal;
}

.markdown-body pre code {
  display: inline;
  max-width: initial;
  padding: 0;
  margin: 0;
  overflow: initial;
  line-height: inherit;
  word-wrap: normal;
  background-color: transparent;
  border: 0;
}

.markdown-body pre code:before,
.markdown-body pre code:after {
  content: normal;
}

/* Admonition */
.markdown-body .admonition {
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  position: relative;
  border-radius: 3px;
  border: 1px solid #e0e0e0;
  border-left: 6px solid #333;
  padding: 10px 10px 10px 30px;
}

.markdown-body .admonition table {
  color: #333;
}

.markdown-body .admonition p {
  padding: 0;
}

.markdown-body .admonition-title {
  font-weight: bold;
  margin: 0;
}

.markdown-body .admonition>.admonition-title {
  color: #333;
}

.markdown-body .attention>.admonition-title {
  color: #a6d796;
}

.markdown-body .caution>.admonition-title {
  color: #d7a796;
}

.markdown-body .hint>.admonition-title {
  color: #96c6d7;
}

.markdown-body .danger>.admonition-title {
  color: #c25f77;
}

.markdown-body .question>.admonition-title {
  color: #96a6d7;
}

.markdown-body .note>.admonition-title {
  color: #d7c896;
}

.markdown-body .admonition:before,
.markdown-body .attention:before,
.markdown-body .caution:before,
.markdown-body .hint:before,
.markdown-body .danger:before,
.markdown-body .question:before,
.markdown-body .note:before {
  font: normal normal 16px fontawesome-mini;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  line-height: 1.5;
  color: #333;
  position: absolute;
  left: 0;
  top: 0;
  padding-top: 10px;
  padding-left: 10px;
}

.markdown-body .admonition:before {
  content: "\f056\00a0";
  color: 333;
}

.markdown-body .attention:before {
  content: "\f058\00a0";
  color: #a6d796;
}

.markdown-body .caution:before {
  content: "\f06a\00a0";
  color: #d7a796;
}

.markdown-body .hint:before {
  content: "\f05a\00a0";
  color: #96c6d7;
}

.markdown-body .danger:before {
  content: "\f057\00a0";
  color: #c25f77;
}

.markdown-body .question:before {
  content: "\f059\00a0";
  color: #96a6d7;
}

.markdown-body .note:before {
  content: "\f040\00a0";
  color: #d7c896;
}

.markdown-body .admonition::after {
  content: normal;
}

.markdown-body .attention {
  border-left: 6px solid #a6d796;
}

.markdown-body .caution {
  border-left: 6px solid #d7a796;
}

.markdown-body .hint {
  border-left: 6px solid #96c6d7;
}

.markdown-body .danger {
  border-left: 6px solid #c25f77;
}

.markdown-body .question {
  border-left: 6px solid #96a6d7;
}

.markdown-body .note {
  border-left: 6px solid #d7c896;
}

.markdown-body .admonition>*:first-child {
  margin-top: 0 !important;
}

.markdown-body .admonition>*:last-child {
  margin-bottom: 0 !important;
}

/* progress bar*/
.markdown-body .progress {
  display: block;
  width: 300px;
  margin: 10px 0;
  height: 24px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  background-color: #ededed;
  position: relative;
  box-shadow: inset -1px 1px 3px rgba(0, 0, 0, .1);
}

.markdown-body .progress-label {
  position: absolute;
  text-align: center;
  font-weight: bold;
  width: 100%; margin: 0;
  line-height: 24px;
  color: #333;
  text-shadow: 1px 1px 0 #fefefe, -1px -1px 0 #fefefe, -1px 1px 0 #fefefe, 1px -1px 0 #fefefe, 0 1px 0 #fefefe, 0 -1px 0 #fefefe, 1px 0 0 #fefefe, -1px 0 0 #fefefe, 1px 1px 2px #000;
  -webkit-font-smoothing: antialiased !important;
  white-space: nowrap;
  overflow: hidden;
}

.markdown-body .progress-bar {
  height: 24px;
  float: left;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  background-color: #96c6d7;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .5), inset 0 -1px 0 rgba(0, 0, 0, .1);
  background-size: 30px 30px;
  background-image: -webkit-linear-gradient(
    135deg, rgba(255, 255, 255, .4) 27%,
    transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%,
    transparent 77%, transparent
  );
  background-image: -moz-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: -ms-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: -o-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
}

.markdown-body .progress-100plus .progress-bar {
  background-color: #a6d796;
}

.markdown-body .progress-80plus .progress-bar {
  background-color: #c6d796;
}

.markdown-body .progress-60plus .progress-bar {
  background-color: #d7c896;
}

.markdown-body .progress-40plus .progress-bar {
  background-color: #d7a796;
}

.markdown-body .progress-20plus .progress-bar {
  background-color: #d796a6;
}

.markdown-body .progress-0plus .progress-bar {
  background-color: #c25f77;
}

.markdown-body .candystripe-animate .progress-bar{
  -webkit-animation: animate-stripes 3s linear infinite;
  -moz-animation: animate-stripes 3s linear infinite;
  animation: animate-stripes 3s linear infinite;
}

@-webkit-keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

@-moz-keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

@keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

.markdown-body .gloss .progress-bar {
  box-shadow:
    inset 0 4px 12px rgba(255, 255, 255, .7),
    inset 0 -12px 0 rgba(0, 0, 0, .05);
}

/* MultiMarkdown Critic Blocks */
.markdown-body .critic_mark {
  background: #ff0;
}

.markdown-body .critic_delete {
  color: #c82829;
  text-decoration: line-through;
}

.markdown-body .critic_insert {
  color: #718c00 ;
  text-decoration: underline;
}

.markdown-body .critic_comment {
  color: #8e908c;
  font-style: italic;
}

.markdown-body .headeranchor {
  font: normal normal 16px fontawesome-mini;
  line-height: 1;
  display: inline-block;
  text-decoration: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.headeranchor:before {
  content: '\e157';
}

.markdown-body .task-list-item {
  list-style-type: none;
}

.markdown-body .task-list-item+.task-list-item {
  margin-top: 3px;
}

.markdown-body .task-list-item input {
  margin: 0 4px 0.25em -20px;
  vertical-align: middle;
}

.markdown-body diagram-div, .markdown-body div.uml-sequence-diagram, .markdown-body, div.uml-flowchart {
  overflow: auto;
}

/* Media */
@media only screen and (min-width: 480px) {
  .markdown-body {
    font-size:14px;
  }
}

@media only screen and (min-width: 768px) {
  .markdown-body {
    font-size:16px;
  }
}

@media print {
  .markdown-body * {
    background: transparent !important;
    color: black !important;
    filter:none !important;
    -ms-filter: none !important;
  }

  .markdown-body {
    font-size:12pt;
    max-width:100%;
    outline:none;
    border: 0;
  }

  .markdown-body a,
  .markdown-body a:visited {
    text-decoration: underline;
  }

  .markdown-body .headeranchor-link {
    display: none;
  }

  .markdown-body a[href]:after {
    content: " (" attr(href) ")";
  }

  .markdown-body abbr[title]:after {
    content: " (" attr(title) ")";
  }

  .markdown-body .ir a:after,
  .markdown-body a[href^="javascript:"]:after,
  .markdown-body a[href^="#"]:after {
    content: "";
  }

  .markdown-body pre {
    white-space: pre;
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  .markdown-body pre,
  .markdown-body blockquote {
    border: 1px solid #999;
    padding-right: 1em;
    page-break-inside: avoid;
  }

  .markdown-body .progress,
  .markdown-body .progress-bar {
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
  }

  .markdown-body .progress {
    border: 1px solid #ddd;
  }

  .markdown-body .progress-bar {
    height: 22px;
    border-right: 1px solid #ddd;
  }

  .markdown-body tr,
  .markdown-body img {
    page-break-inside: avoid;
  }

  .markdown-body img {
    max-width: 100% !important;
  }

  .markdown-body p,
  .markdown-body h2,
  .markdown-body h3 {
    orphans: 3;
    widows: 3;
  }

  .markdown-body h2,
  .markdown-body h3 {
    page-break-after: avoid;
  }
}
</style><style>/*GitHub*/
.highlight {background-color:#fff;color:#333333;}
.highlight .hll {background-color:#ffffcc;}
.highlight .c{color:#999988;font-style:italic}
.highlight .err{color:#a61717;background-color:#e3d2d2}
.highlight .k{font-weight:bold}
.highlight .o{font-weight:bold}
.highlight .cm{color:#999988;font-style:italic}
.highlight .cp{color:#999999;font-weight:bold}
.highlight .c1{color:#999988;font-style:italic}
.highlight .cs{color:#999999;font-weight:bold;font-style:italic}
.highlight .gd{color:#000000;background-color:#ffdddd}
.highlight .ge{font-style:italic}
.highlight .gr{color:#aa0000}
.highlight .gh{color:#999999}
.highlight .gi{color:#000000;background-color:#ddffdd}
.highlight .go{color:#888888}
.highlight .gp{color:#555555}
.highlight .gs{font-weight:bold}
.highlight .gu{color:#800080;font-weight:bold}
.highlight .gt{color:#aa0000}
.highlight .kc{font-weight:bold}
.highlight .kd{font-weight:bold}
.highlight .kn{font-weight:bold}
.highlight .kp{font-weight:bold}
.highlight .kr{font-weight:bold}
.highlight .kt{color:#445588;font-weight:bold}
.highlight .m{color:#009999}
.highlight .s{color:#dd1144}
.highlight .n{color:#333333}
.highlight .na{color:teal}
.highlight .nb{color:#0086b3}
.highlight .nc{color:#445588;font-weight:bold}
.highlight .no{color:teal}
.highlight .ni{color:purple}
.highlight .ne{color:#990000;font-weight:bold}
.highlight .nf{color:#990000;font-weight:bold}
.highlight .nn{color:#555555}
.highlight .nt{color:navy}
.highlight .nv{color:teal}
.highlight .ow{font-weight:bold}
.highlight .w{color:#bbbbbb}
.highlight .mf{color:#009999}
.highlight .mh{color:#009999}
.highlight .mi{color:#009999}
.highlight .mo{color:#009999}
.highlight .sb{color:#dd1144}
.highlight .sc{color:#dd1144}
.highlight .sd{color:#dd1144}
.highlight .s2{color:#dd1144}
.highlight .se{color:#dd1144}
.highlight .sh{color:#dd1144}
.highlight .si{color:#dd1144}
.highlight .sx{color:#dd1144}
.highlight .sr{color:#009926}
.highlight .s1{color:#dd1144}
.highlight .ss{color:#990073}
.highlight .bp{color:#999999}
.highlight .vc{color:teal}
.highlight .vg{color:teal}
.highlight .vi{color:teal}
.highlight .il{color:#009999}
.highlight .gc{color:#999;background-color:#EAF2F5}
</style><title>JUC 并发编程 （尚硅谷）2021 基础篇</title></head><body><article class="markdown-body"><h2 id="_1">相关资源<a class="headerlink" href="#_1" title="Permanent link"></a></h2>
<p><a href="https://www.bilibili.com/video/BV1Kw411Z7dF">【尚硅谷】大厂必备技术之 JUC 并发编程</a></p>
<p>课程时间: 07:41:26</p>
<h2 id="_2">目标<a class="headerlink" href="#_2" title="Permanent link"></a></h2>
<p>[] 第一天：01 - 17 （2021年6月19日18:01 - 2021/06/20_18:01）</p>
<p>[] 第二天: 18 - 30 </p>
<p>[] 第三天: 31 - 43 </p>
<h2 id="_3">概述 与 基础知识<a class="headerlink" href="#_3" title="Permanent link"></a></h2>
<ul>
<li>
<p>JUC 指 Java.util.concurrent 工具包，JUC 是它的简称，这是一个处理线程的工具包，JDK 1.5 开始出现</p>
</li>
<li>
<p>进程和线程</p>
</li>
<li>
<p>进程（Process） 是系统进行资源分配和调度的基本单位，操作系统结构的基础。</p>
<p>进程是计算机中的程序关于某数据集合上的一次运行活动，是系统进行资源分配和调度的基本单位，是操作系统结构的基础。在当代面向线程设计的计算机结构中，进程是线程的容器。程序是指令、数据及其组织形式的描述，进程是程序的实体。是计算机中的程序关于某数据集合上的一次运行活动，是系统进行资源分配和调度的基本单位，是操作系统结构的基础。</p>
</li>
<li>
<p>线程（thread）  </p>
<p>线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中，是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流，一个进程中可以并发多个线程，每条线程并行执行不同的任务。</p>
</li>
</ul>
<p>总结来说:.</p>
<p>进程∶ 指在系统中正在运行的一个应用程序, 程序一旦运行就是进程。进程 —— 资源分配的最小单位。·</p>
<p>线程: 系统分配处理器时间资源的基本单元，或者说进程之内独立执行的一个单元执行流。线程 —— 程序执行的最小单位。。</p>
<ul>
<li>线程的状态</li>
</ul>
<p>可以通过 <code>Thread.State</code> 类查看</p>
<div class="highlight"><pre><span class="kd">public</span> <span class="kd">enum</span> <span class="n">State</span> <span class="o">{</span>
      <span class="cm">/**</span>
<span class="cm">       * // 新建，表示一个还没有启动的线程</span>
<span class="cm">       */</span> 
      <span class="n">NEW</span><span class="o">,</span> 

      <span class="n">RUNNABLE</span><span class="o">,</span> <span class="c1">// 准备就绪（可运行的，正在 Java 虚拟机中执行，但可能在等待操作系统（如 处理器）的其他资源）</span>

      <span class="n">BLOCKED</span><span class="o">,</span> <span class="c1">// 阻塞</span>

      <span class="n">WAITING</span><span class="o">,</span> <span class="c1">// 一直等</span>

      <span class="n">TIMED_WAITING</span><span class="o">,</span> <span class="c1">// 有时效性（指定时间）的等</span>

      <span class="n">TERMINATED</span><span class="o">;</span> <span class="c1">// 终结，表示执行完成</span>
  <span class="o">}</span>
</pre></div>

<ul>
<li>
<p>两个方法 wait 和 sleep</p>
</li>
<li>
<p>区别</p>
<ol>
<li>wait 是 Object 中方法</li>
</ol>
<p>sleep 是 Thread 的静态方法</p>
<ol start="2">
<li>sleep 不会释放锁，也不需要占用锁。</li>
</ol>
<p>wait 会释放锁，但调用的前提是当前线程占有锁（即代码要在 synchronized 中）</p>
<p>wait 会放开锁去睡觉，sleep 会抓住锁去睡觉</p>
</li>
<li>
<p>同</p>
<p>都可以被 interrupted 方法中断。</p>
<p>在哪里睡就会在哪里醒</p>
</li>
<li>
<p>并发和并行</p>
</li>
</ul>
<p>串行: 串一串，依次运行</p>
<p>并行: 并排，一起，各回各家，多个对应多个，（真同时）</p>
<p>并发: 同时跟很多人聊天，短时间内不断切换，一对多。（假同时，）</p>
<ul>
<li>管程（）</li>
</ul>
<p>管程 Monitor 监视器，锁</p>
<p>是一种同步机制，保证同一时间，只能有一个线程对被保护的资源（数据/代码）访问</p>
<p>同步操作要基于进入退出 Monitor（管程） 对象 <!-- 完成 -->来实现的，</p>
<!-- 会随 Java 对象一起创建和销毁 -->

<ul>
<li>用户线程 和 守护线程</li>
</ul>
<p>用户线程: 用户自定义线程</p>
<p>守护线程: 特殊的在后台运行的线程。比如垃圾回收。</p>
<p><code>Thread.currentThread().isDeamon()</code> 方法，返回 <code>flase</code> 表示为用户线程，<code>true</code> 表示为守护线程</p>
<p>当没有用户线程 了，（主线程也是用户线程）JVM 就会结束。</p>
<h2 id="lock">Lock 接口<a class="headerlink" href="#lock" title="Permanent link"></a></h2>
<ul>
<li>Synchronized </li>
</ul>
<p>是 Java 中重要的关键字，是一种同步锁。修饰的对象有以下几种：</p>
<ol>
<li>
<p>代码块</p>
<p>被修饰的代码块称为同步语句块，其作用范围是大括号括起来的代码，作用的对象是调用这个代码块的对象</p>
<p>如：</p>
<div class="highlight"><pre><span class="kd">synchronized</span><span class="o">(</span><span class="k">this</span><span class="o">)</span> <span class="o">{</span>
  <span class="c1">// ...</span>
<span class="o">}</span>
</pre></div>

</li>
<li>
<p>方法</p>
<p>被修饰的方法称为同步方法，其作用范围是整个方法，作用对象是调用这个方法的对象。</p>
<ul>
<li>虽然可以使用 synchronized 来定义方法，但 synchronized 并不属于方法定义的一部分，因此，synchronized 关键字不能被继承。如果在父类中的某个方法使用了 synchronized 关键字，而在子类中覆盖了这个方法，在子类中的这个方法默认情况下并不是同步的，而必须显式地在子类的这个方法中加上 synchronized 关键字才可以。当然，还可以在子类方法中调用父类中相应的方法，这样虽然子类中的方法不是同步的，但子类调用了父类的同步方法，因此子类的方法也就相当于同步了。</li>
</ul>
</li>
<li>
<p>静态方法，</p>
<p>其作用的范围是整个静态方法，作用的对象是这个类的所有对象</p>
</li>
<li>
<p>类，</p>
<p>其作用的范围是 synchronized 后面括号括起来的部分，作用主的对象是这个类的所有对象。。</p>
</li>
</ol>
<ul>
<li>Lock 接口</li>
</ul>
<p>参见 jdk 手册</p>
<p>不同于 Synronized 自动实现 锁（上锁解锁），也可以自己手动控制</p>
<p>Lock 实现提供了比使用 Synchronized 方法和语句客户的的更广泛的锁定操作。此实现允许更灵活的结构，可以具有差别很大的属性，可以支持多个相关的 <code>Condition</code> 对象。（参见 jdk 文档 <code>java.util.concurrent.locks</code> 包，及其下的 <code>Lock</code> 接口）</p>
<ul>
<li>Lock 与的Synchronized区别
&lt;!&ndash; <br />
  Lock 不是Java语言内置的，synchronized是Java语言的关键字，因此是内置特性。Lock是一个类，通过这个类可以实现同步访问;</li>
</ul>
<p>Lock和synchronized有一点非常大的不同，采用synchrgnized不需要用户去手动释放锁，当synchronized方法或者synchronized 代码块执行完之后，系统会自动让线程释放对锁的占用;</p>
<p>而Lock则必须要用户去手动释放锁，如果没有主动释放锁，就有可能导致出现死锁现象。 &ndash;&gt;</p>
<ol>
<li>
<p>Lock是一个接口，而synchronized是Java中的关键字，synchronized是内置的语言实现;</p>
</li>
<li>
<p>synchronized在发生异常时，会自动释放线程占有的锁，因此不会导致死锁现
  象发生。而Lock在发生异常时，如果没有主动通过unLock()去释放锁，则很可能造成死锁现象，因此使用Lock时需要在finally块中释放锁;。</p>
</li>
<li>
<p>Lock 可以让等待锁的线程响应中断，而 synchronized 却不行，使用
  synchronized 时，等待的线程会一直等待下去，不能够响应中断;。</p>
</li>
<li>
<p>通过 Lock 可以知道有没有成功获取锁，而 synchronized 却无法办到。,</p>
</li>
<li>
<p>Lock 可以提高多个线程进行读操作的效率。。</p>
</li>
</ol>
<p>在性能上来说，如果竞争资源不激烈，两者的性能是差不多的，而当竞争资源非常激烈时（即有大量线程同时竞争），此时 Lock 的性能要远远优于 synchronized</p>
<h3 id="_4">多线程编程步骤（上）<a class="headerlink" href="#_4" title="Permanent link"></a></h3>
<ol>
<li>创建资源类，→ 在资源类创建属性， 和 操作方法</li>
<li>创建多个线程，→ 调用资源类的操作方法</li>
</ol>
<ul>
<li>高内聚，低耦合，各司其职，线程只调用，<span style='color: #999'>不在线程资源里再去创建，</span><span style='color: #ccc'>如 3 个售票员，卖 30张票</span></li>
</ul>
<details>
  <summary>举例代码</summary>

  <details>
    <summary>通过 synchronized 关键字</summary>

    <div class="highlight"><pre><span class="n">package</span> <span class="n">sing</span><span class="p">.</span><span class="n">sync</span><span class="p">;</span>

<span class="k">public</span> <span class="k">class</span> <span class="n">SaleTicket</span> <span class="err">{</span>
    <span class="k">public</span> <span class="k">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span> <span class="err">{</span>
        <span class="o">//</span> <span class="err">创建资源类</span> <span class="n">Ticket</span>
        <span class="n">Ticket</span> <span class="n">ticket</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Ticket</span><span class="p">();</span>
        <span class="o">//</span> <span class="err">创建线程</span>
        <span class="k">new</span> <span class="n">Thread</span><span class="p">(()</span> <span class="o">-&gt;</span> <span class="err">{</span>
            <span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">40</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="err">{</span>
                <span class="o">//</span> <span class="err">调用</span>
                <span class="n">ticket</span><span class="p">.</span><span class="n">sale</span><span class="p">();</span>
            <span class="err">}</span>
        <span class="err">}</span><span class="p">,</span> <span class="ss">&quot;aa&quot;</span><span class="p">).</span><span class="k">start</span><span class="p">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="p">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="p">()</span> <span class="err">{</span>
            <span class="o">@</span><span class="n">Override</span>
            <span class="k">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="err">{</span>
                <span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">40</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="err">{</span>
                    <span class="o">//</span> <span class="err">调用</span>
                    <span class="n">ticket</span><span class="p">.</span><span class="n">sale</span><span class="p">();</span>
                <span class="err">}</span>
            <span class="err">}</span>
        <span class="err">}</span><span class="p">,</span> <span class="ss">&quot;bb&quot;</span><span class="p">).</span><span class="k">start</span><span class="p">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="p">(()</span> <span class="o">-&gt;</span> <span class="err">{</span>
            <span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">40</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="err">{</span>
                <span class="o">//</span> <span class="err">调用</span>
                <span class="n">ticket</span><span class="p">.</span><span class="n">sale</span><span class="p">();</span>
            <span class="err">}</span>
        <span class="err">}</span><span class="p">,</span> <span class="ss">&quot;vv&quot;</span><span class="p">).</span><span class="k">start</span><span class="p">();</span>
    <span class="err">}</span>
<span class="err">}</span>


<span class="o">//</span> <span class="err">资源类</span>
<span class="k">class</span> <span class="n">Ticket</span> <span class="err">{</span>
    <span class="o">//</span> <span class="err">属性</span><span class="p">:</span> <span class="err">数量</span>
    <span class="n">private</span> <span class="nb">int</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">30</span><span class="p">;</span>

    <span class="o">//</span> <span class="err">操作方法：售卖</span>
    <span class="k">public</span> <span class="n">synchronized</span> <span class="n">void</span> <span class="n">sale</span><span class="p">()</span> <span class="err">{</span>
        <span class="n">if</span><span class="p">(</span><span class="n">num</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="err">{</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="err">}</span>
        <span class="k">System</span><span class="p">.</span><span class="k">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">currentThread</span><span class="p">().</span><span class="n">getName</span><span class="p">()</span> <span class="o">+</span> <span class="ss">&quot;剩余：&quot;</span> <span class="o">+</span> <span class="c1">-- num);</span>
<span class="o">//</span>        <span class="n">num</span> <span class="c1">--;</span>
    <span class="err">}</span>
<span class="err">}</span>
</pre></div>

  </details>

<p><details>
    <summary>通过 Lock 接口</summary></p>
<pre><code>```java
package sing.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 + @author xing
 + @date 2022/6/20 10:57
 */
public class LSaleTicket {
    public static void main(String[] args) {
        //
        LTicket ticket = new LTicket();
        new Thread(() -&gt; {
            for (int i = 0; i &lt; 40; i++) {
                // 调用
                ticket.sale();
            }
        }, "aa").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i &lt; 40; i++) {
                    // 调用
                    ticket.sale();
                }
            }
        }, "bb").start();

        new Thread(() -&gt; {
            for (int i = 0; i &lt; 40; i++) {
                // 调用
                ticket.sale();
            }
        }, "vv").start();
    }
}

class LTicket {
    // 属性
    private int num = 30;
    // 创建可重入锁
    private final ReentrantLock lock = new ReentrantLock();
    // 操作方法
    public void sale() {
        // 加锁
        lock.lock();
        // try..catch..防止异常导致锁没有释放，造成的资源始终被占用
        try {
            if(num &lt;= 0) {
                return;
            }
            System.out.println(Thread.currentThread().getName() + "剩余：" + -- num);
        } finally {
            // 释放锁
            lock.unlock();
        }

    }
}

```
</code></pre>
</details>

</details>

<ol start="3">
<li>1</li>
</ol>
<h3 id="thead">Thead 细节<a class="headerlink" href="#thead" title="Permanent link"></a></h3>
<p><code>.start</code> 方法不一定马上创建线程，它是调用本地方法，由操作系统创建，如果操作系统空闲，马上创建，不空闲，一会儿创建</p>
<h2 id="_5">线程间通信<a class="headerlink" href="#_5" title="Permanent link"></a></h2>
<h3 id="_6">概述<a class="headerlink" href="#_6" title="Permanent link"></a></h3>
<h3 id="sychronired">Sychronired<a class="headerlink" href="#sychronired" title="Permanent link"></a></h3>
<h3 id="_7">虚假唤醒问题<a class="headerlink" href="#_7" title="Permanent link"></a></h3>
<blockquote>
<p>虚假唤醒 （spurious wakeup）</p>
</blockquote>
<ul>
<li><code>wait()</code> 方法，，在哪里睡觉就会在哪里苏醒</li>
</ul>
<p>带来的问题就是 <code>wait()</code> 方法前的内容醒来后都不会执行了，会继续往下执行。</p>
<ul>
<li>
<p>那么什么是虚假唤醒呢？</p>
</li>
<li>
<p>虚假唤醒</p>
<p>虚假唤醒可以简单的说为，</p>
<p>被唤醒了，但不做事，又继续 wait 等待，可以称为虚假唤醒（虚晃一枪，醒了，骗你的，醒了但没完全醒）</p>
<blockquote>
<p>不做事指不是目标线程，被唤醒发现，这件事不应该它做，继续等待。</p>
</blockquote>
</li>
<li>
<p>虚假唤醒问题</p>
<p>其可能带来的问题，就是虚假唤醒问题，</p>
<p>例如本来不应该做事，继续睡觉，但是它并没有，而是做了事儿，那就坏了，<!-- 很有可能 --><!-- 导致 -->这是意料外情况，会带来意料外的问题。由此可能带来的问题就是虚假唤醒问题。</p>
</li>
</ul>
<p>当一个条件满足时，很多线程都被唤醒了，但是只有其中部分是有用的唤醒，</p>
<p>其它的唤醒都应该是无用功，但这些其他的唤醒并没有按我们想象的来，他们也做了一些事情，导致出现问题。</p>
<ul>
<li>
<p>举例</p>
<!-- 比如说买货，如果商品本来没有货物，突然进了一件商品，这是所有的线程都被唤醒了，但是只能一个人买，所以其他人都是假唤醒，获取不到对象的锁 -->

<p>例如一个方法锁（加了锁的方法）中，只有一个判断，里边是 wait 方法，本来期望的是本来是 0 加为 1，</p>
<p>但是被唤醒的是本来应该无用的线程，又因为 从 wait 方法往下执行，外层是判断，直接就成功往下执行了，导致出现意料外问题/操作。</p>
<p><span style='color: #ccc'>(如果外边线程调用，包裹在循环内，可能还会导致错误的累积（因为错误（代码/流程）可能会被执行多次）)</span></p>
</li>
</ul>
<p>所以<!-- 一般 -->为了防止这种情况，<!-- 方法内第一步就是判断（<a href="#多线程编程步骤（中）">多线程编程步骤（中</a>） -->，用 while 包裹 <code>wait</code> 方法。</p>
<blockquote>
<p>所以如果 <code>wait</code> 方法在一个判断中，醒来后就会从睡的地方苏醒，那么该 <code>wait</code> 方法前执行的内容，包括外层判断就失效了，。</p>
<p>而如果是 for 循环，前面已经循环的次数不会重置。</p>
</blockquote>
<h4 id="_8">中间遇到的问题<a class="headerlink" href="#_8" title="Permanent link"></a></h4>
<div class="highlight"><pre><span class="kn">package</span> <span class="nn">sing.sync</span><span class="o">;</span>

<span class="cm">/**</span>
<span class="cm"> * @author xing</span>
<span class="cm"> * @date 2022/6/20 12:26</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TheadDemo</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">Share</span> <span class="n">share</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Share</span><span class="o">();</span>
        <span class="c1">// 创建线程</span>
        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;aa i: &quot;</span> <span class="o">+</span> <span class="n">i</span><span class="o">);</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">incr</span><span class="o">();</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;aa&quot;</span> <span class="o">+</span> <span class="s">&quot;线程结束&quot;</span><span class="o">);</span>
        <span class="o">},</span> <span class="s">&quot;aa&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;bb i: &quot;</span> <span class="o">+</span> <span class="n">i</span><span class="o">);</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">decr</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;bb&quot;</span> <span class="o">+</span> <span class="s">&quot;线程结束&quot;</span><span class="o">);</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;BB&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;cc i: &quot;</span> <span class="o">+</span> <span class="n">i</span><span class="o">);</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">decr</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;cc&quot;</span> <span class="o">+</span> <span class="s">&quot;线程结束&quot;</span><span class="o">);</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;CC&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Dd i: &quot;</span> <span class="o">+</span> <span class="n">i</span><span class="o">);</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">decr</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;dd&quot;</span> <span class="o">+</span> <span class="s">&quot;线程结束&quot;</span><span class="o">);</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;DD&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">class</span> <span class="nc">Share</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>

    <span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">incr</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行准备&quot;</span><span class="o">);</span>
        <span class="c1">// 判断</span>
        <span class="k">while</span><span class="o">(</span><span class="n">num</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行等待&quot;</span><span class="o">);</span>
            <span class="k">this</span><span class="o">.</span><span class="na">wait</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="c1">// 干活</span>
        <span class="n">num</span> <span class="o">++;</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;:&quot;</span> <span class="o">+</span> <span class="n">num</span><span class="o">);</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行成功，通知其他线程&quot;</span><span class="o">);</span>

        <span class="c1">// 通知</span>
        <span class="k">this</span><span class="o">.</span><span class="na">notifyAll</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">decr</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行准备&quot;</span><span class="o">);</span>
        <span class="c1">// 判断</span>
        <span class="k">while</span><span class="o">(</span><span class="n">num</span> <span class="o">!=</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行等待&quot;</span><span class="o">);</span>
            <span class="k">this</span><span class="o">.</span><span class="na">wait</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="c1">// 干活</span>
        <span class="n">num</span> <span class="o">--;</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;:&quot;</span> <span class="o">+</span> <span class="n">num</span><span class="o">);</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;执行成功，通知其他线程&quot;</span><span class="o">);</span>
        <span class="c1">// 通知</span>
        <span class="k">this</span><span class="o">.</span><span class="na">notifyAll</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>

<p>因为只有 一个线程是 增，其他都是减，增的执行完了，线程结束，剩下三个减的没有，就会一直等待，</p>
<p>这里是属于死锁么，还是什么</p>
<p>以及好像，<code>notifyAll</code> 唤醒后，线程都会继续往下执行（都被唤醒，但是 <code>start</code> 方法启动，同时期只能运行一个），不过前面例子中，抢到资源的如果是目标线程，又会执行 <code>notifyAll</code> 在加上，是 <code>start</code> 方法执行，不是并发的，所以难以搞懂，</p>
<p>上边错误例子中，最后 aa 线程结束后，可以看到三个线程，依次执行进入等待，aa 的 <code>notifyAll</code> 后线程结束，导致一直等待，没有被唤醒。</p>
<p>所以 <code>notifyAll</code> 方法后，如果被唤醒线程都进入等待，（或结束，我猜的）执行该方法的线程会继续执行（有点像 <code>.join</code> 方法，等待其他目标线程完成后，继续本线程）。</p>
<h3 id="_9">多线程编程步骤（中）<a class="headerlink" href="#_9" title="Permanent link"></a></h3>
<ol>
<li>
<p>第一步 创建资源类，在资源类创建属性和操作方法</p>
</li>
<li>
<p>第二步 在资源类操作方法</p>
</li>
<li>
<p>判断</p>
</li>
<li>干活</li>
<li>
<p>通知</p>
</li>
<li>
<p>第三步 创建多个线程，调用资源类的操作方法</p>
</li>
</ol>
<blockquote>
<p>在 <code>Object</code> 类中，有这几个方法：</p>
<ul>
<li><code>notify()</code></li>
</ul>
<p>唤醒在此对象监视器上等待的单个线程</p>
<blockquote>
<p>此方法只应由作为此对象监视器的所有者的线程来调用。通过以下三种方法之一，线程可以成为此对象监视器的所有者：</p>
<ul>
<li>
<p>通过执行此对象的同步实例方法。</p>
</li>
<li>
<p>通过执行在此对象上进行同步的 synchronized 语句的正文。</p>
</li>
<li>
<p>对于 Class 类型的对象，可以通过执行该类的同步静态方法。</p>
</li>
</ul>
<p>一次只能有一个线程拥有对象的监视器。</p>
</blockquote>
<ul>
<li><code>notifyAll()</code></li>
</ul>
<p>唤醒在此对象监视器上等待的所有线程</p>
<blockquote>
<p>此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述，请参阅 notify 方法。</p>
</blockquote>
<ul>
<li><code>wait()</code></li>
</ul>
<p>导致当前线程等待，直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法</p>
<ul>
<li><code>wait(long timeout)</code></li>
</ul>
<p>导致当前线程等待，直到或者超过指定的时间量前，其他线程调用此对象的 notify() 方法或 notifyAll() 方法。</p>
<ul>
<li><code>wait(long timeout, int nanos)</code></li>
</ul>
<p>导致当前线程等待，直到其他线程其他某个线程中断当前线程，或者已超过某个实示时间量，或调用此对象的 notify() 方法或 notifyAll() 方法。</p>
</blockquote>
<details>
  <summary>例子</summary>

  有两个线程，实现对一个初始值为0的变量，

  一个线程对值 + 1，

  另外一个线程对值 - 1

  参考代码：

  通过 Synchronized

  <div class="highlight"><pre><span class="kn">package</span> <span class="nn">sing.sync</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TheadDemo</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">Share</span> <span class="n">share</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Share</span><span class="o">();</span>
        <span class="c1">// 创建线程</span>
        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">incr</span><span class="o">();</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;aa&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">decr</span><span class="o">();</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;BB&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">class</span> <span class="nc">Share</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>

    <span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">incr</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
        <span class="c1">// 判断</span>
        <span class="k">if</span><span class="o">(</span><span class="n">num</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="na">wait</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="c1">// 干活</span>
        <span class="n">num</span> <span class="o">++;</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;:&quot;</span> <span class="o">+</span> <span class="n">num</span><span class="o">);</span>
        <span class="c1">// 通知</span>
        <span class="k">this</span><span class="o">.</span><span class="na">notifyAll</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">decr</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
        <span class="c1">// 判断</span>
        <span class="k">if</span><span class="o">(</span><span class="n">num</span> <span class="o">!=</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="na">wait</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="c1">// 干活</span>
        <span class="n">num</span> <span class="o">--;</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;:&quot;</span> <span class="o">+</span> <span class="n">num</span><span class="o">);</span>
        <span class="c1">// 通知</span>
        <span class="k">this</span><span class="o">.</span><span class="na">notifyAll</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>

</details>

<h3 id="_10">多线程编程步骤（下）<a class="headerlink" href="#_10" title="Permanent link"></a></h3>
<ol>
<li>
<p>第一步 创建资源类，在资源类创建属性和操作方法</p>
</li>
<li>
<p>第二步 在资源类操作方法</p>
</li>
<li>
<p>判断</p>
</li>
<li>干活</li>
<li>
<p>通知</p>
</li>
<li>
<p>第三步 创建多个线程，调用资源类的操作方法</p>
</li>
<li>
<p>第四步 防止虚假唤醒问题</p>
</li>
</ol>
<h2 id="_11">线程的定制化通信<a class="headerlink" href="#_11" title="Permanent link"></a></h2>
<p>指各线程按约定的顺序执行</p>
<p>可以有多种实现方案，这里只说一种比较通用的</p>
<ul>
<li>通过设置一个标志位，让标志位符合的线程执行。</li>
</ul>
<p>例如三个线程，一个标志位 flag, 三个线程分别对应值 1,2,3 ，先判断标志位，让 AA 执行，然后对标志位修改，通知其他线程，让下一个指定线程执行（其他例如可能设置多个标志位，不管怎样都是自己写代码的考虑，其他情况<!-- 在 -->再其他考虑，要思考，没什么是万能的）</p>
<p><strong>这里的关键也就是让其他非指定线程的唤醒均为虚假唤醒，确保按预期执行，达到预期效果。或可以直接只通知指定线程，唤醒指定线程。（判断还是要的）</strong></p>
<p>与前面例子中的一个加一，一个减1的循环判断，异曲同工，可以看做是一样的，前者判断的是本身的属性值，后边判断的是额外的标志位，其实前者例子中，也可以看做是标志位。</p>
<details>
  <summary>示例1</summary>

  <div class="highlight"><pre><span class="kn">package</span> <span class="nn">sing.sync</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.concurrent.locks.Lock</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.concurrent.locks.ReentrantLock</span><span class="o">;</span>

<span class="cm">/**</span>
<span class="cm"> * @author xing</span>
<span class="cm"> * @date 2022/6/24 10:42</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ThreadDemo3</span> <span class="o">{</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
        <span class="n">Share</span> <span class="n">share</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Share</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">next</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>

                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;11&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>

                <span class="n">share</span><span class="o">.</span><span class="na">next</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;aa&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>

                <span class="n">share</span><span class="o">.</span><span class="na">next</span><span class="o">(</span><span class="mi">3</span><span class="o">);</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;bb&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="kd">static</span> <span class="kd">class</span> <span class="nc">Share</span> <span class="o">{</span>
        <span class="kd">private</span> <span class="kt">int</span> <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="c1">//        private final Lock lock = new ReentrantLock();</span>

        <span class="c1">// 这个名字刚开始是想错了，后来没改名字</span>
        <span class="kd">public</span> <span class="kd">synchronized</span> <span class="kt">void</span> <span class="nf">next</span><span class="o">(</span><span class="kt">int</span> <span class="n">thisFlag</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
            <span class="c1">// 判断</span>
            <span class="k">while</span> <span class="o">(</span><span class="n">flag</span> <span class="o">!=</span> <span class="n">thisFlag</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">this</span><span class="o">.</span><span class="na">wait</span><span class="o">();</span>
            <span class="o">}</span>

            <span class="c1">// 干活</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;flag: &quot;</span> <span class="o">+</span> <span class="n">flag</span> <span class="o">+</span> <span class="s">&quot;执行&quot;</span><span class="o">);</span>
            <span class="n">flag</span><span class="o">++;</span>
            <span class="k">if</span><span class="o">(</span><span class="n">flag</span> <span class="o">==</span> <span class="mi">4</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c1">// 通知</span>
            <span class="k">this</span><span class="o">.</span><span class="na">notifyAll</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</details>

<details>
  <summary>示例二</summary>

  <div class="highlight"><pre><span class="kn">package</span> <span class="nn">sing.sync</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.concurrent.locks.Condition</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.concurrent.locks.Lock</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.concurrent.locks.ReentrantLock</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ThreadDemo4</span> <span class="o">{</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">Share</span> <span class="n">share</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Share</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">print5</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;aa&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">print10</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;bb&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">share</span><span class="o">.</span><span class="na">print15</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
                <span class="o">}</span>
            <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">InterruptedException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">},</span> <span class="s">&quot;cc&quot;</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="kd">static</span> <span class="kd">class</span> <span class="nc">Share</span> <span class="o">{</span>
        <span class="kd">private</span> <span class="kt">int</span> <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
        <span class="kd">private</span> <span class="kd">final</span> <span class="n">Lock</span> <span class="n">lock</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ReentrantLock</span><span class="o">();</span>

        <span class="n">Condition</span> <span class="n">c1</span> <span class="o">=</span> <span class="n">lock</span><span class="o">.</span><span class="na">newCondition</span><span class="o">();</span>
        <span class="n">Condition</span> <span class="n">c2</span> <span class="o">=</span> <span class="n">lock</span><span class="o">.</span><span class="na">newCondition</span><span class="o">();</span>
        <span class="n">Condition</span> <span class="n">c3</span> <span class="o">=</span> <span class="n">lock</span><span class="o">.</span><span class="na">newCondition</span><span class="o">();</span>

        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">print5</span><span class="o">(</span><span class="kt">int</span> <span class="n">loop</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
            <span class="n">lock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">while</span> <span class="o">(</span><span class="n">flag</span> <span class="o">!=</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span>
                    <span class="n">c1</span><span class="o">.</span><span class="na">await</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;::&quot;</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="s">&quot;轮数：&quot;</span> <span class="o">+</span> <span class="n">loop</span><span class="o">);</span>
                <span class="o">}</span>
                <span class="n">flag</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
                <span class="n">c2</span><span class="o">.</span><span class="na">signal</span><span class="o">();</span>

            <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
                <span class="n">lock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">}</span>

        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">print10</span><span class="o">(</span><span class="kt">int</span> <span class="n">loop</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
            <span class="n">lock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">while</span> <span class="o">(</span><span class="n">flag</span> <span class="o">!=</span> <span class="mi">2</span><span class="o">)</span> <span class="o">{</span>
                    <span class="n">c2</span><span class="o">.</span><span class="na">await</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;::&quot;</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="s">&quot;轮数：&quot;</span> <span class="o">+</span> <span class="n">loop</span><span class="o">);</span>
                <span class="o">}</span>
                <span class="n">flag</span> <span class="o">=</span> <span class="mi">3</span><span class="o">;</span>
                <span class="n">c3</span><span class="o">.</span><span class="na">signal</span><span class="o">();</span>

            <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
                <span class="n">lock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">}</span>

        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">print15</span><span class="o">(</span><span class="kt">int</span> <span class="n">loop</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">InterruptedException</span> <span class="o">{</span>
            <span class="n">lock</span><span class="o">.</span><span class="na">lock</span><span class="o">();</span>
            <span class="k">try</span> <span class="o">{</span>
                <span class="k">while</span> <span class="o">(</span><span class="n">flag</span> <span class="o">!=</span> <span class="mi">3</span><span class="o">)</span> <span class="o">{</span>
                    <span class="n">c3</span><span class="o">.</span><span class="na">await</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">15</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
                    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&quot;::&quot;</span> <span class="o">+</span> <span class="s">&quot;i&quot;</span> <span class="o">+</span> <span class="s">&quot;轮数：&quot;</span> <span class="o">+</span> <span class="n">loop</span><span class="o">);</span>
                <span class="o">}</span>
                <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
                <span class="n">c1</span><span class="o">.</span><span class="na">signal</span><span class="o">();</span>

            <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
                <span class="n">lock</span><span class="o">.</span><span class="na">unlock</span><span class="o">();</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</details>

<h2 id="_12">集合线程的安全<a class="headerlink" href="#_12" title="Permanent link"></a></h2>
<h3 id="_13">异常演示<a class="headerlink" href="#_13" title="Permanent link"></a></h3>
<p><a href="https://www.bilibili.com/video/BV1Kw411Z7dF?p=14">视频地址</a></p>
<p>由于 ArrayList 类中的 add 方法没有加锁，</p>
<p>如果多个线程对它操作，写的同时又去读，就会报错。</p>
<details>
  <summary>举例</summary>

  <div class="highlight"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ThreadDemo5</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>

        <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">30</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
            <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
                <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">UUID</span><span class="o">.</span><span class="na">randomUUID</span><span class="o">().</span><span class="na">toString</span><span class="o">().</span><span class="na">substring</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="mi">8</span><span class="o">));</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">list</span><span class="o">);</span>
            <span class="o">},</span> <span class="n">String</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">i</span><span class="o">)).</span><span class="na">start</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>

</details>

<p>下边列出几个解决办法</p>
<h3 id="vector">Vector<E><a class="headerlink" href="#vector" title="Permanent link"></a></h3>
<p>我们将上边代码中的 <code>new ArrayList&lt;&gt;()</code> 修改为 <code>new Vector&lt;&gt;()</code></p>
<details>
  <summary>示例</summary>

  <div class="highlight"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ThreadDemo5</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>

        <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">30</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
            <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
                <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">UUID</span><span class="o">.</span><span class="na">randomUUID</span><span class="o">().</span><span class="na">toString</span><span class="o">().</span><span class="na">substring</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="mi">8</span><span class="o">));</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">list</span><span class="o">);</span>
            <span class="o">},</span> <span class="n">String</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">i</span><span class="o">)).</span><span class="na">start</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</details>

<p>发现就不报错了。通过源码，我们可以看到，它的  方法前都加了 <code>synchronized</code> 关键字。</p>
<p>但用的并不多，比较古老，这是 JDK 1.0 出现的方案。</p>
<h3 id="collections">Collections<a class="headerlink" href="#collections" title="Permanent link"></a></h3>
<p>通过 Collections 中的静态方法，</p>
<p>我们再修改前面代码，修改为 <code>List&lt;String&gt; list = Collections.synchronizedList(new ArrayList&lt;&gt;());</code></p>
<p>发现也是可以的，</p>
<p>不过这个方案也是比较古老的，使用几率不是特别大</p>
<p>实际经常用的是 JUC 中的方案</p>
<h3 id="copyonwri">CopyOnWri<a class="headerlink" href="#copyonwri" title="Permanent link"></a></h3></article></body></html>